package cn.blunce.greendaoinandroidstudio.base;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import java.util.List;

import cn.blunce.greendaoinandroidstudio.models.DaoMaster;
import cn.blunce.greendaoinandroidstudio.models.DaoSession;
import cn.blunce.greendaoinandroidstudio.models.Job;
import cn.blunce.greendaoinandroidstudio.models.JobDao;
import cn.blunce.greendaoinandroidstudio.models.Person;
import cn.blunce.greendaoinandroidstudio.models.PersonDao;

/**
 * Simple add, delete, modify, query in database.
 * <p/>
 * Created by Experiment on 2016/4/21 0021.
 */
public class DBHelper {
    private static DaoMaster.DevOpenHelper helper;
    private static SQLiteDatabase mDatabase;
    private static DaoMaster mDaoMaster;
    private static DaoSession mDaoSession;
    private static PersonDao mPersonDao;
    private static JobDao mJobDao;
    private static Context mContext;

    private static DBHelper sDBHelper = null;

    private DBHelper(Context context) {
        mContext = context;
        helper = new DaoMaster.DevOpenHelper(context, "db", null);
        mDatabase = helper.getWritableDatabase();
        mDaoMaster = new DaoMaster(mDatabase);
        mDaoSession = mDaoMaster.newSession();
        mPersonDao = mDaoSession.getPersonDao();
        mJobDao = mDaoSession.getJobDao();
    }

    public static synchronized DBHelper getInstance(Context context) {
        if (sDBHelper == null)
            sDBHelper = new DBHelper(context);
        return sDBHelper;
    }

    /**
     * Object is added to the database.
     *
     * @param object
     * @return
     */
    public boolean add(Object object) {
        boolean tag = true;
        if (new Person().getClass().getName() == object.getClass().getName()) {
            mPersonDao.insert((Person) object);
        } else if (new Job().getClass().getName() == object.getClass().getName()) {
            mJobDao.insert((Job) object);
        } else
            tag = false;
        return tag;
    }

    public void deletePersonById(Long id) {
        mPersonDao.deleteByKey(id);
    }

    public void deleteJobByCategory(String category) {
        mJobDao.deleteByKey(category);
    }

    /**
     * Object is modified.
     *
     * @param object
     * @return
     */
    public boolean modifyObject(Object object) {
        boolean tag = true;
        if (new Person().getClass().getName() == object.getClass().getName()) {
            mPersonDao.insertOrReplace((Person) object);
        } else if (new Job().getClass().getName() == object.getClass().getName()) {
            mJobDao.insertOrReplace((Job) object);
        } else
            tag = false;
        return tag;
    }

    public List<Person> queryPersonByAge(int age) {
        return mPersonDao.queryBuilder().where(PersonDao.Properties.Age.eq(age)).list();
    }

    public List<Job> queryJobByContent(String content) {
        return mJobDao.queryBuilder().where(JobDao.Properties.Content.eq(content)).list();
    }

    public List<Person> getAllPersons() {
        return mPersonDao.loadAll();
    }

    public List<Job> getAllJobs() {
        return mJobDao.loadAll();
    }

}
